package com.rocsea.robot.template.impl;

import com.rocsea.robot.config.build.KettleBuildConfig;
import com.rocsea.robot.config.build.KettleBuildTemplateConfig;
import com.rocsea.robot.config.build.TemplateInfo;
import com.rocsea.robot.factory.KettleTemplateFactory;
import com.rocsea.robot.model.entity.StepAttributeDO;
import com.rocsea.robot.model.enums.KettleBuildStrategyEnum;
import com.rocsea.robot.service.KettlePackService;
import com.rocsea.robot.service.KettleTaskConfigService;
import com.rocsea.robot.template.AbstractTemplate;
import com.rocsea.robot.model.bo.KpBuildInfo;
import com.rocsea.robot.utils.MysqlBuilderUtils;
import com.rocsea.robot.model.entity.TransformationDO;
import com.rocsea.robot.utils.BeanCopyUtils;
import com.rocsea.robot.utils.KettleBuildUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * 简单的同步
 * @Author RocSea
 * @Date 2022/12/7
 */
@Component
public class SimpleTemplate extends AbstractTemplate {
    @Resource
    private KettleBuildTemplateConfig templateConfig;

    @Resource
    protected KettlePackService kettlePackService;

    @Resource
    protected KettleTaskConfigService kettleTaskConfigService;

    @Resource
    private KettleBuildConfig buildConfig;

    public KettleBuildTemplateConfig getTemplateConfig() {
        return templateConfig;
    }

    @Override
    public TemplateInfo getTemplateInfo() {
        return templateConfig.getSimple();
    }

    @Override
    public String processTransformation(TransformationDO transformationDO) {
        TransformationDO buildDO = BeanCopyUtils.copyProperties(transformationDO, TransformationDO.class);
        buildDO.setIdTransformation(KettleBuildUtils.getIdMaxTransRelated().addAndGetIdTransformationMax());
        buildDO.setIdDirectory(KettleBuildUtils.getIdDirectory());
        buildDO.setName("sync_" + KettleBuildUtils.getSourceTableName());
        return MysqlBuilderUtils.buildTransformation(buildDO);
    }

    @Override
    public String processTableInput(List<StepAttributeDO> stepAttributeDOS) throws IOException {
        return super.processTableInputWithRowMeta(stepAttributeDOS);
    }

    @Override
    public String processSqlReplace(String sql) {
        String replace = sql.replace(getTemplateInfo().getName(), KettleBuildUtils.getSourceTableName());
        return replace.substring(replace.indexOf("FROM"));
    }

    @Override
    public String processQueryFromTableName(List<StepAttributeDO> stepAttributeDOS) throws IOException {
        return super.processTableInputWithRowMeta(stepAttributeDOS);
    }

    @Override
    public String processKettlePack() {
        String jobParamTemplate = "{\"taskName\":\"%s\",\"compensationTaskName\":\"%s(task_compensation)\"," +
                "\"commit_size\":\"1000\",\"outTableName\":\"%s\"}";
        String jobCompensationParamTemplate = "{\"taskName\":\"%s(task_compensation)\",\"commit_size\":\"1000\"," +
                "\"outTableName\":\"%s\"}";
        KpBuildInfo kpBuildInfo = createKpBuildInfo(buildConfig.getOutDatabase());
        String jobParam = String.format(jobParamTemplate, kpBuildInfo.getTaskName(),
                kpBuildInfo.getTaskName(), kpBuildInfo.getTargetTableName());
        String jobCompensationParam = String.format(jobCompensationParamTemplate, kpBuildInfo.getTaskName(),
                kpBuildInfo.getTargetTableName());
        kpBuildInfo.setJobParam(jobParam);
        kpBuildInfo.setJobCompensationParam(jobCompensationParam);
        return kettlePackService.buildKettlePack(kpBuildInfo);
    }

    @Override
    public String processKettleTaskConfig() throws ParseException {
        return kettleTaskConfigService.buildKettleTaskConfig(true);
    }

    @Override
    public void afterPropertiesSet() {
        KettleTemplateFactory.register(KettleBuildStrategyEnum.SIMPLE.getText(), this);
    }

}
